#include <ani.h>
#include <array>
#include <iostream>

#include "ani_helpers_build_scripts.h"

static ani_long impl_Malloc(ani_long size) { return reinterpret_cast<ani_long>(malloc(size)); }

static void impl_Free(ani_long ptr) {
  return free(reinterpret_cast<char *>(ptr));
  ;
}

ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) {
  ani_env *env{};
  CHECK_ANI_FATAL(vm->GetEnv(ANI_VERSION_1, &env));

  constexpr const char *className = "InteropNativeModuleLight.InteropNativeModuleLight";
  ani_class cls;
  CHECK_ANI_FATAL(env->FindClass(className, &cls));

  std::array methods = {
      ani_native_function{"_Free", "l:", reinterpret_cast<void *>(impl_Free)},
      ani_native_function{"_Malloc", "l:l", reinterpret_cast<void *>(impl_Malloc)},
  };

  CHECK_ANI_FATAL(env->Class_BindStaticNativeMethods(cls, methods.data(), methods.size()));

  *result = ANI_VERSION_1;
  return ANI_OK;
}
